//
//  HealthRecordLogViewController.m
//  安好
//
//  Created by 马晓良 on 14-11-14.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "HealthRecordLogViewController.h"
#import "HealthRecordLogInfoCell.h"
#import "HealthNaviTitleView.h"
#import "HealthMoodViewController.h"
#import "BasicSignsViewController.h"
#import "RespiratoryRateViewController.h"
#import "BodyTemperatureViewController.h"
#import "HeartRateViewController.h"
#import "BloodPressureViewController.h"
#import "BloodSugarViewController.h"
#import "MemoryStepTastViewController.h"
#import "StepCountViewController.h"

@interface HealthRecordLogViewController ()<UITableViewDataSource,UITableViewDelegate>
/**
 *  数据的现实TableView
 */
@property (strong, nonatomic) IBOutlet UITableView *healthRecordInfoTableView;
/**
 *  重要体征数组
 */
@property (nonatomic, strong) NSArray *importantSignsArray;
/**
 *  基本体征
 */
@property (nonatomic, strong) NSArray *basisSignsArray;

/**
 *  自定义的naviBarTitleView;
 */
@property (nonatomic, strong) HealthNaviTitleView *healthNaviTitleView;

@property (nonatomic, strong) NSString *timeStr;

@property (nonatomic, assign) NSInteger status;
@end

@implementation HealthRecordLogViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.status = 1;
    self.healthRecordInfoTableView.dataSource = self;
    self.healthRecordInfoTableView.delegate = self;
    self.healthNaviTitleView = [[[NSBundle mainBundle] loadNibNamed:@"HealthNaviTitleView" owner:self options:nil] lastObject];
    self.navigationItem.titleView = self.healthNaviTitleView;
    self.importantSignsArray = @[@"基础体征"];
    self.healthRecordInfoTableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    self.basisSignsArray = @[@"呼吸速率",@"体温",@"心率",@"血压",@"血糖"];
    [self setNaviBarAttribute];
    self.timeStr = [self setNewTime];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self todayMoodSendMessage];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 获取当前的时间 -
- (NSString *)setNewTime{
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    NSInteger year = [dateComponent year];
    NSInteger month = [dateComponent month];
    NSInteger day = [dateComponent day];
    NSString *times = [NSString stringWithFormat:@"%ld年%ld月%ld日",(long)year,(long)month,(long)day];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
    NSString *locationString=[formatter stringFromDate: [NSDate date]];
    self.healthNaviTitleView.titleViewDate.text = times;
    return locationString;
}

- (NSString *)setNewTimes{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSString *locationString=[formatter stringFromDate: [NSDate date]];
    return locationString;
    
}

#pragma mark - 设置naviBar 并实现左右naviBar button方法 -
- (void)setNaviBarAttribute{
    /**
     *  设置左NaviBar和右naviBar
     */
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 23, 23);
    [leftButton setImage:[UIImage imageNamed:@"公用_返回"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(handleLeftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;

}


/**
 *  返回主页面
 */
- (void)handleLeftButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - tableView的代理方法 -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 1;
    }else if (section == 2){
        return 5;
    }else if (section == 3){
        return 1;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (kScreenWidth == 414) {
        return 54;
    }else{
        return 44;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 0.1;
    }else{
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        if (kScreenWidth == 414) {
            return 54;
        }else{
            return 44;
        }
    }
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor clearColor];
        UITextView *moodHintText = [[UITextView alloc] init];
        moodHintText.backgroundColor = [UIColor clearColor];
        moodHintText.text = @"健康心情每日仅允许发布一条,如果需要发布新的心情,需删除之前的记录方可进行！";
        moodHintText.editable = NO;
        moodHintText.selectable = NO;
        moodHintText.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13];
        if (kScreenWidth == 414) {
            view.frame = CGRectMake(0, 0, kScreenWidth, 54);
            moodHintText.frame = CGRectMake(11, 0, kScreenWidth  - 20, 54);
            
            [view addSubview:moodHintText];
            return view;
        }else{
            view.frame = CGRectMake(0, 0, kScreenWidth, 44);
            moodHintText.frame = CGRectMake(11, 0, kScreenWidth  - 20, 44);
            [view addSubview:moodHintText];
            return view;
        }
    }else{
        return nil;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentify = @"healthRecordLogInfoCell";
    HealthRecordLogInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:indentify];
    if (!cell) {
        if (kScreenWidth == 414) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"HealthRecordLogInfoCell" owner:self options:nil] lastObject];
        }else{
            cell = [[[NSBundle mainBundle] loadNibNamed:@"HealthRecordLogInfoCell" owner:self options:nil] firstObject];
        }
    }
    if (indexPath.section == 0) {
        if (self.status == 1) {
            cell.messageLable.hidden = YES;
            cell.jumpImageView.hidden = NO;
        }else if(self.status == 0){
            cell.messageLable.hidden = NO;
            cell.jumpImageView.hidden = YES;
        }
        cell.healthRecordInfoLabel.text = @"健康心情";
    }else if (indexPath.section == 1){
        cell.healthRecordInfoLabel.text = self.importantSignsArray[indexPath.row];
        cell.messageLable.hidden = YES;
    }else if (indexPath.section == 2){
        cell.healthRecordInfoLabel.text = self.basisSignsArray[indexPath.row];
        cell.messageLable.hidden = YES;
    }else{
        cell.healthRecordInfoLabel.text = @"记步任务";
        cell.messageLable.hidden = YES;
    }
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        HealthMoodViewController *healthMoodVC = [[HealthMoodViewController alloc] init];
        healthMoodVC.timeStr = self.healthNaviTitleView.titleViewDate.text;
        if (self.status == 1) {
            [self.navigationController pushViewController:healthMoodVC animated:YES];
        }else{
        }
    }else if(indexPath.section == 1){
        BasicSignsViewController *basicSignsVC = [[BasicSignsViewController alloc] init];
        basicSignsVC.timeStr = self.timeStr;
        [self.navigationController pushViewController:basicSignsVC animated:YES];
    }else if (indexPath.section == 2){
        if (indexPath.row == 0) {
            RespiratoryRateViewController *respiratoryRateVC = [[RespiratoryRateViewController alloc] init];
            respiratoryRateVC.timeStr = self.timeStr;
            [self.navigationController pushViewController:respiratoryRateVC animated:YES];
        }else if (indexPath.row == 1){
            BodyTemperatureViewController *bodyTemperatureVC = [[BodyTemperatureViewController alloc] init];
            bodyTemperatureVC.timeStr = self.timeStr;
            [self.navigationController pushViewController:bodyTemperatureVC animated:YES];
        }else if (indexPath.row == 2){
            HeartRateViewController *heartRateVC = [[HeartRateViewController alloc] init];
            heartRateVC.timeStr = self.timeStr;
            [self.navigationController pushViewController: heartRateVC animated:YES];
        }else if (indexPath.row == 3){
            BloodPressureViewController *bloodPressurVC = [[BloodPressureViewController alloc] init];
            bloodPressurVC.timeStr = self.timeStr;
            [self.navigationController pushViewController:bloodPressurVC animated:YES];
        }else{
            BloodSugarViewController *bloodSugarVC = [[BloodSugarViewController alloc] init];
            bloodSugarVC.timeStr = self.timeStr;
            [self.navigationController pushViewController:bloodSugarVC animated:YES];
        }
    }else if (indexPath.section == 3){
//        MemoryStepTastViewController *memoryStepTastVC = [[MemoryStepTastViewController alloc] init];
//        [self.navigationController pushViewController:memoryStepTastVC animated:YES];
        StepCountViewController *walk;
        if (kScreenHeight == 480) {
            walk = [[StepCountViewController alloc]initWithNibName:@"StepCountViewController" bundle:nil];
        }
        else if (kScreenHeight == 568){
            walk = [[StepCountViewController alloc]initWithNibName:@"StepCountViewController_5" bundle:nil];
        }
        else if (kScreenHeight == 667){
            walk = [[StepCountViewController alloc]initWithNibName:@"StepCountViewController_6" bundle:nil];
        }
        else if (kScreenHeight == 736){
            walk = [[StepCountViewController alloc]initWithNibName:@"StepCountViewController_6+" bundle:nil];
        }
        [self.navigationController pushViewController:walk animated:YES];
    }
}

#pragma mark - 验证健康心情今天是否已发送 -
- (void)todayMoodSendMessage{
//    [self showActionView];
    NSString * postURL = [NSString stringWithFormat:@"%@log/checkdescriptionpic/",kBaseURL];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:kPlatform forKey:@"platform"];
    [dic setValue:kVersion forKey:@"version"];
    [manager POST:postURL parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        [self hiddenActionView];
        self.status = [[responseObject objectForKey:@"status"] integerValue];
        [self.healthRecordInfoTableView reloadData];
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        MyNSLog("网络失败");
//        [self hiddenActionView];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
